package com.yjq.yys.yhCalculate.controller;

import com.yjq.yys.yhCalculate.service.YhService;
import com.yjq.yys.yhCalculate.utils.ReadJson;
import com.yjq.yys.yhCalculate.vo.YhCalculateVO;
import com.yjq.yys.yhCalculate.vo.YhExpectVO;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author yjq
 * @since 2019-11-19
 */
@RequestMapping("/v1/yh")
@RestController
@Api("御魂控制层")
@Slf4j
public class YhController {

    @Resource
    private YhService yhService;

    @GetMapping("/calculate")
    public List<YhCalculateVO> calculate(YhExpectVO expect) {
        long beginTime = System.currentTimeMillis();
        List<YhCalculateVO> result = yhService.calculate(expect);
        log.info("---总计耗时: " + (System.currentTimeMillis() - beginTime) / 1000 + "s.");
        return result;
    }

    @PostMapping("/importJson")
    public String importJson(MultipartFile file) {
        try {
            return yhService.addByJson(ReadJson.readJson(file.getInputStream()), 1);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "添加失败";
    }

    @PutMapping("/use")
    public String user(String idListStr, Boolean isUsable) {
        yhService.use(Stream.of(idListStr.split(",")).map(Integer::valueOf).collect(Collectors.toList()), isUsable);
        return "应用成功";
    }

    @PutMapping("/allCanUse")
    public Integer user() {
        return yhService.allCanUse(1);
    }

}
